package com.wcar.vipmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.Store;
import com.wcar.common.entity.UserBalanceLog;
import com.wcar.common.vo.Result;
import com.wcar.vipmanagement.dao.StoreDao;
import com.wcar.vipmanagement.dao.UserBalanceLogDao;

import com.wcar.vipmanagement.service.UserBalanceLogServicce;
import com.wcar.vipmanagement.vo.allVo;
import com.wcar.vipmanagement.vo.yueVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @program: four-wcar-idea
 * @description:
 * @author: 张云龙
 * @create: 2024-06-04 20:13
 **/
@Service
public class UserBalanceLogServicceImpl implements UserBalanceLogServicce {

	@Autowired
	private UserBalanceLogDao userBalanceLogDao;
	@Autowired
	private StoreDao storeDao ;

	@Override
	public Result selectBalanceByUserId(String id) {
		Integer accountMoney = 0;
		try {
			QueryWrapper<UserBalanceLog> wrapper1 = new QueryWrapper<>();
			wrapper1.eq("user_id",id);
			wrapper1 .select("sum(account_money) as accountMoney");
			UserBalanceLog userBalanceLog1 = userBalanceLogDao.selectOne(wrapper1);
			if(userBalanceLog1!=null) {
				accountMoney = userBalanceLog1.getAccountMoney();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		Integer accountMoney1 = 0;
		try {
			QueryWrapper<UserBalanceLog> wrapper2 = new QueryWrapper<>();
			wrapper2.eq("user_id",id);
			wrapper2 .select("sum(expenditure) as expenditure");
			UserBalanceLog userBalanceLog2 = userBalanceLogDao.selectOne(wrapper2);
			if(userBalanceLog2!=null) {
				accountMoney1 = userBalanceLog2.getExpenditure();
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		String s = String.valueOf(accountMoney - accountMoney1);


	/*	QueryWrapper<UserBalanceLog> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", id);
		wrapper.select("sum(account_money) as accountMoney");
		UserBalanceLog userBalanceLog = userBalanceLogDao.selectOne(wrapper);
		Integer accountMoney = userBalanceLog.getAccountMoney();*/


		HashMap<Object, Object> hashMap = new HashMap<>();
		hashMap.put("balance",s);
		return new Result(200, "查询用户余额", hashMap);
	}

	@Override
	public Result<allVo>  selectBalanceDetail(String id) {
		QueryWrapper<UserBalanceLog> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", id);
			List<UserBalanceLog> userBalanceLogs = userBalanceLogDao.selectList(wrapper);
			//加
		QueryWrapper<UserBalanceLog> wrapper1 = new QueryWrapper<>();
		wrapper1.eq("user_id", id);
		wrapper1.eq("expenditure_time",null);
		List<UserBalanceLog> userBalanceLogs1 = userBalanceLogDao.selectList(wrapper);
			//减
		QueryWrapper<UserBalanceLog> wrapper2 = new QueryWrapper<>();
		wrapper2.eq("user_id", id);
		wrapper2.eq("recharge_time",null);
		List<UserBalanceLog> userBalanceLogs2 = userBalanceLogDao.selectList(wrapper);
		ArrayList<yueVo> objects = new ArrayList<>();
		ArrayList<yueVo> objects1 = new ArrayList<>();
		ArrayList<yueVo> objects2 = new ArrayList<>();
		for (UserBalanceLog a: userBalanceLogs) {
			QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
			storeQueryWrapper.eq("store_id",a.getStoreId());
			Store store = storeDao.selectOne(storeQueryWrapper);
			if (store!=null) {
			yueVo yueVo = new yueVo();

			yueVo.setAccountMoney(a.getAccountMoney());
			yueVo.setBalanceLogId(a.getBalanceLogId());
			yueVo.setDiscountsMoney(a.getDiscountsMoney() );
			yueVo.setExpenditure(a.getExpenditure());
			yueVo.setExpenditureTime(a.getExpenditureTime());
			yueVo.setRechargeMoney(a.getRechargeMoney());
			yueVo.setRechargeTime(a.getRechargeTime());
			yueVo.setStoreId(a.getStoreId());
			yueVo.setStoreName(store.getStoreName());
			yueVo.setUserId(a.getUserId());
			objects.add(yueVo);
			}
		}
		for (UserBalanceLog a: userBalanceLogs1) {
			QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
			storeQueryWrapper.eq("store_id",a.getStoreId());
			Store store = storeDao.selectOne(storeQueryWrapper);
			if (store!=null) {
				yueVo yueVo = new yueVo();
				yueVo.setAccountMoney(a.getAccountMoney());
				yueVo.setBalanceLogId(a.getBalanceLogId());
				yueVo.setDiscountsMoney(a.getDiscountsMoney());
				yueVo.setExpenditure(a.getExpenditure());
				yueVo.setExpenditureTime(a.getExpenditureTime());
				yueVo.setRechargeMoney(a.getRechargeMoney());
				yueVo.setRechargeTime(a.getRechargeTime());
				yueVo.setStoreId(a.getStoreId());
				yueVo.setStoreName(store.getStoreName());
				yueVo.setUserId(a.getUserId());
				objects1.add(yueVo);
			}
		}
		for (UserBalanceLog a: userBalanceLogs2) {
			QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
			storeQueryWrapper.eq("store_id",a.getStoreId());
			Store store = storeDao.selectOne(storeQueryWrapper);
			if (store!=null) {
				yueVo yueVo = new yueVo();
				yueVo.setAccountMoney(a.getAccountMoney());
				yueVo.setBalanceLogId(a.getBalanceLogId());
				yueVo.setDiscountsMoney(a.getDiscountsMoney());
				yueVo.setExpenditure(a.getExpenditure());
				yueVo.setExpenditureTime(a.getExpenditureTime());
				yueVo.setRechargeMoney(a.getRechargeMoney());
				yueVo.setRechargeTime(a.getRechargeTime());
				yueVo.setStoreId(a.getStoreId());
				yueVo.setStoreName(store.getStoreName());
				yueVo.setUserId(a.getUserId());
				objects2.add(yueVo);
			}
		}
		allVo allVo = new allVo();
		allVo.setAll(objects);
		allVo.setIn(objects1);
		allVo.setOut(objects2);

		return new Result(200,"拿到余额明细",allVo);
	}
}
